package main

import (
	"flag"
	"github.com/micro/go-micro/v2"
	log "github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	"github.com/micro/go-plugins/wrapper/select/roundrobin/v2"
	"rpc-service/product/config"
	"rpc-service/product/datasource"
	"rpc-service/product/handler"
)

var (
	//指定配置文件路径,默认使用当前目录下的config.ini文件
	configPath = flag.String("config", "./config.ini", "-config file path")
)

func InitServer() {
	//解析命令运行参数
	flag.Parse()
	//从config.json加载配置信息
	log.Info("Config Path:", *configPath)
	config.InitConfig(*configPath)
	//连接数据库
	datasource.InitDatabase()
}

func main() {
	InitServer()
	conf := config.GetConfig()
	//注册中心
	registry := etcd.NewRegistry(registry.Addrs(conf.RegistryAddr))
	//负载均衡策略:轮循
	wrapper := roundrobin.NewClientWrapper()
	service := micro.NewService(
		micro.Address(":"+conf.Port),
		micro.Name(conf.ServiceName),
		micro.Version(conf.Version),
		micro.Registry(registry),
		micro.WrapClient(wrapper),
	)
	service.Init()
	//注册grpc handler
	handler.RegisterServiceHandlers(service.Server())
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
